from numba import jit
import numpy as np
import cython

#@jit
@cython.wraparound(False)
@cython.boundscheck(False)
@cython.cdivision(True)
def cal(data1, data2, hist1, hist2, hist3, hist4, int cursor_1_1, int cursor_1_2, int cursor_2_1, int cursor_2_2, int cursor_3_1, int cursor_3_2, int cursor_4_1, int cursor_4_2,int mode):

    cdef int i
    if mode == 1:
        data4 = np.zeros(shape=(100000, 2))
        data5 = np.zeros(shape=(100000, 2))
        data6 = np.zeros(shape=(100000, 2))
        data7 = np.zeros(shape=(100000, 2))
        for i in range(100000):
            data4[i][0] = i  # 第一列从0-99999编号 第二列为 0
            data5[i][0] = i
            data6[i][0] = i
            data7[i][0] = i
    elif mode == 2:
        data4 = hist1
        data5 = hist2
        data6 = hist3
        data7 = hist4
    else:
        pass

    cdef int sweeps = 0
    cdef int det1 = 0
    cdef int det2 = 0
    cdef int det3 = 0
    cdef int det4 = 0
    cdef int count1 = 0
    cdef int count2 = 0
    cdef int count3 = 0
    cdef int count4 = 0
    cdef int coin12 = 0
    cdef int coin13 = 0
    cdef int coin23 = 0
    cdef int coin123 = 0
    a = np.arange(0, len(data1), 1)
    cdef long sweep
    cdef int x
    for i in a:      # 采集到的数据格式为两列，第一列为到来时间，第二列为通道编号   数据是按行读取的    如：18965,1
        if data2[i] == 1 and -1 < data1[i] < 100000:                                      # 1955,2
            x = int(data1[i])
            data4[x][1] += 1
            if cursor_1_1 < data1[i] < cursor_1_2:
                det1 += 1       # det:每个周期中单通道标记符，理论上只有0和1，实际可能出现多个，还是算作1，每当一个新的周期开始，将它们清零
                count1 += 1     # count:整个测量时间内的计数
        elif data2[i] == 2 and -1 < data1[i] < 100000:
            x = int(data1[i])
            data5[x][1] += 1
            if cursor_2_1 < data1[i] < cursor_2_2:
                det2 += 1
                count2 += 1
        elif data2[i] == 3 and -1 < data1[i] < 100000:
            x = int(data1[i])
            data6[x][1] += 1
            if cursor_3_1 < data1[i] < cursor_3_2:
                det3 += 1
                count3 += 1
        elif data2[i] == 4 and -1 < data1[i] < 100000:  # 以4作为标识符  每当一个4到来  意味着一个新的周期开始 计算两个4之间的通道 1、2、3之间的计数(1,2)(1,3)(1,2,3)
            sweeps += 1  # sweeps为探测周期数
            x = int(data1[i])                           # 如果并发 则这几个通道的符合计数+1 这些情况可能发生一种或多种 也可能不发生
            data7[x][1] += 1
            if cursor_4_1 < data1[i] < cursor_4_2:
                det4 += 1
                count4 += 1
            if det1 != 0 and det2 != 0:
                coin12 += 1
                if det3 != 0:
                    coin13 += 1
                    coin23 += 1
                    coin123 += 1
            elif det1 != 0 and det3 != 0:
                coin13 += 1
            elif det2 != 0 and det3 != 0:
                coin23 += 1
            else:
                pass
            det1 = 0
            det2 = 0
            det3 = 0
            det4 = 0

    return sweeps, count1, count2, count3, coin12, coin13, coin23, coin123, data4, data5, data6, count4, data7